﻿Imports System.Runtime.CompilerServices

Public Module IDbCommandExtension

#Region "ExecuteReader"
    ''' <summary>
    ''' ログ付きのExecuteReader
    ''' </summary>
    ''' <param name="source"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function ExecuteReaderWithLog(source As IDbCommand, Optional level As InfoLevelEnum = InfoLevelEnum.Info) As IDataReader
        Return ExecuteReaderWithLog(source, String.Empty, level)
    End Function

    ''' <summary>
    ''' ログ付きのExecuteReader
    ''' </summary>
    ''' <param name="source"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function ExecuteReaderWithLog(source As IDbCommand, tableName As String, Optional level As InfoLevelEnum = InfoLevelEnum.Info) As IDataReader
        Log.DoExecuteCommandAction(source, source, tableName, level)
        Try
            Return source.ExecuteReader
        Catch ex As Exception
            '#99 ロギングをNLogにする
            Dim new_ex As New QueryRuntimeException(ex)
            Log.DoExceptionAction(source, source, new_ex)
            Throw new_ex
        End Try
    End Function
#End Region

#Region "ExecuteScalarWithLog"
    ''' <summary>
    ''' ログ付きのExecuteScalar
    ''' </summary>
    ''' <param name="source"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function ExecuteScalarWithLog(source As IDbCommand, Optional level As InfoLevelEnum = InfoLevelEnum.Info) As Object
        Log.DoExecuteCommandAction(source, source, level)
        Try
            Return source.ExecuteScalar
        Catch ex As Exception
            '#99 ロギングをNLogにする
            Dim new_ex As New QueryRuntimeException(ex)
            Log.DoExceptionAction(source, source, new_ex)
            Throw new_ex
        End Try
    End Function
#End Region

#Region "ExecuteNonQueryWithLog"
    ''' <summary>
    ''' ログ付きのExecuteNonQuery
    ''' </summary>
    ''' <param name="source"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function ExecuteNonQueryWithLog(source As IDbCommand, Optional level As InfoLevelEnum = InfoLevelEnum.Info) As Integer
        Return ExecuteNonQueryWithLog(source, String.Empty, level)
    End Function

    ''' <summary>
    ''' ログ付きのExecuteNonQuery
    ''' </summary>
    ''' <param name="source"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function ExecuteNonQueryWithLog(source As IDbCommand, tableName As String, Optional level As InfoLevelEnum = InfoLevelEnum.Info) As Integer
        Log.DoExecuteCommandAction(source, source, tableName, level)
        Try
            Return source.ExecuteNonQuery
        Catch ex As Exception
            '#99 ロギングをNLogにする
            Dim new_ex As New QueryRuntimeException(ex)
            Log.DoExceptionAction(source, source, new_ex)
            Throw new_ex
        End Try
    End Function
#End Region

End Module